#pragma once

#include "base/MessageLoop.h"
#include <zmqpp/reactor.hpp>
#include <zmqpp/zmqpp.hpp>

namespace zeroplus::mq
{
    typedef std::function<void(void)> Callable;
    class Reactor
    {
    private:
        zmqpp::reactor reactor_;
        MessageLoop messageLoop_;

    public:
        Reactor() {}
        ~Reactor()
        {
            Stop();
        }

        void Add(zmqpp::socket_t &socket, Callable callable, short const event) // Callable
        {
            reactor_.add(socket, callable, event);
        }

        bool Start()
        {
            // 启动事件循环
            auto handler = [this]()
            {
                while (true)
                {
                    reactor_.poll();
                }
            };
            messageLoop_.post_task(handler);
            messageLoop_.run();
        }

        bool Stop()
        {
            messageLoop_.quit();
            messageLoop_.wait_for_quit();
        }
    };
}
